﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.ENT;
using SIS.BLL;

public partial class school_enquiry : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadSchoolList();
            ddlSchoolList.DataBind();
            if (ddlSchoolList.Items.Count == 0)
            {
                lblMessage.Text = "Please set up a school profile firstly!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }

            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.Text = System.Configuration.ConfigurationManager.AppSettings["SchoolKeyDefault"].ToString();

                //ddlSchoolList.SelectedIndex = 0;
                //ddlSchoolList_SelectedIndexChanged(sender, e);
            }

            ddlRelationShipType.Items.Insert(0, new ListItem("Selft", "0"));
            ddlEnquiryReasonType.Items.Insert(0, new ListItem("Select", "-1"));
            ddlEnquiryType.Items.Insert(0, new ListItem("Select", "-1"));
        }
    }

    //private void LoadSchoolList()
    //{
    //    ddlSchoolList.Items.Clear();
    //    SchoolProfileTableList _profileList = SchoolProfileTableManager.GetList();
    //    if (_profileList != null)
    //    {
    //        foreach (SchoolProfileTable _profile in _profileList)
    //        {
    //            ddlSchoolList.Items.Add(new ListItem(_profile.SchoolName, _profile.SchoolKey.ToString()));
    //        }
    //    }
    //}

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlLocation.DataBind();
        ClearFields();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            EnquiryTable _enquiry = new EnquiryTable();
            _enquiry.EnquiryKey = Convert.ToInt32(lblEnquiryKey.Text);
            lblSchoolKey.Text = ddlSchoolList.Text;
            lblLocationKey.Text = ddlLocation.Text;
            _enquiry.SchoolKey = Convert.ToInt32(lblSchoolKey.Text);
            _enquiry.LocationKey = Convert.ToInt32(lblLocationKey.Text);
            _enquiry.Status = 1; //chkStatus.Checked;

            _enquiry.FirstName = txtFirstName.Text;
            _enquiry.LastName = txtLastName.Text;
            _enquiry.OtherName = txtOtherName.Text;
            _enquiry.Gender = ddlGender.Text;
            _enquiry.EnquiryType = Convert.ToInt16(ddlEnquiryType.Text);
            _enquiry.EnquiryReasonType = Convert.ToInt16(ddlEnquiryReasonType.Text);
            _enquiry.RelationshipTypeCode = Convert.ToInt16(ddlRelationShipType.Text);

            _enquiry.Message = txtMessage.Text;
            _enquiry.Remark = txtRemark.Text;
            _enquiry.ContactEmail = txtContactEmail.Text;
            _enquiry.ContactPhoneNumber = txtContactPhoneNo.Text;
            _enquiry.ContactAddress = txtUnitNo.Text.Replace("|", "") + "|" +
                                        txtStreetName.Text.Replace("|", "") + "|" +
                                        ddlCity.SelectedItem.Text + "|" +
                                        ddlDistrict.SelectedItem.Text + "|" +
                                        ddlCountry.SelectedItem.Text + "|" +
                                        txtPostCode.Text.Replace("|", "");

            _enquiry.PreferContactTypeCode = Convert.ToInt16(ddlPreferContactType.Text);

            if (EnquiryTableManager.Save(_enquiry) > 0)
            {
                ClearFields();
                grvEnquiry.DataBind();
                lblPageMessage.Text = "Saved sussccessfully!";
                lblPageMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblPageMessage.Text = "Saving failed!";
                lblPageMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (lblEnquiryKey.Text.Length > 0)
        {
            if (EnquiryTableManager.Delete(Convert.ToInt32(lblEnquiryKey.Text)))
            {
                ClearFields();
                grvEnquiry.DataBind();
                lblPageMessage.Text = "Item was deleted!";
                lblPageMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblPageMessage.Text = "Deleting failed!";
                lblPageMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        else
        {
            lblPageMessage.Text = "Please select an item to delete.";
            lblPageMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void grvEnquiry_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        if (grvEnquiry.SelectedIndex >= 0)
        {
            DataKey _selectedDataKey = grvEnquiry.SelectedDataKey;
            int _enquiryKey = Convert.ToInt32(_selectedDataKey[0]);
            int _schoolKey = Convert.ToInt32(_selectedDataKey[1]);
            int _locationKey = Convert.ToInt32(_selectedDataKey[2]);

            if (_enquiryKey > 0 && _schoolKey > 0 && _locationKey > 0)
            {

                EnquiryTable _enquiry = new EnquiryTable();
                _enquiry = EnquiryTableManager.GetItem(_enquiryKey);
                if (_enquiry != null)
                {
                    btnCreateStudentProfile.Visible = true;
                    lblEnquiryKey.Text = _enquiryKey.ToString();
                    lblSchoolKey.Text = _schoolKey.ToString();
                    lblLocationKey.Text = _locationKey.ToString();
                    txtFirstName.Text = _enquiry.FirstName;
                    txtLastName.Text = _enquiry.LastName;
                    txtOtherName.Text = _enquiry.OtherName;

                    ddlGender.Text = _enquiry.Gender;
                    ddlEnquiryType.Text = _enquiry.EnquiryType.ToString();
                    ddlEnquiryReasonType.Text = _enquiry.EnquiryReasonType.ToString();
                    txtMessage.Text = _enquiry.Message;
                    txtRemark.Text = _enquiry.Remark;

                    txtContactEmail.Text = _enquiry.ContactEmail;
                    txtContactPhoneNo.Text = _enquiry.ContactPhoneNumber;
                    ddlRelationShipType.Text = _enquiry.RelationshipTypeCode.ToString();
                    ddlPreferContactType.Text = _enquiry.PreferContactTypeCode.ToString();

                    if (_enquiry.ContactAddress.Length > 0)
                    {
                        string[] _address = _enquiry.ContactAddress.Split('|');
                        if (_address.Length == 6)
                        {
                            txtUnitNo.Text = _address[0];
                            txtStreetName.Text = _address[1];

                            ListItem _lstCity = ddlCity.Items.FindByText(_address[2]);
                            if (_lstCity != null)
                            {
                                ddlCity.Text = _lstCity.Value.ToString();
                            }

                            ListItem _lstDistrict = ddlDistrict.Items.FindByText(_address[3]);
                            if (_lstDistrict != null)
                            {
                                ddlDistrict.Text = _lstDistrict.Value.ToString();
                            }

                            ListItem _lstCountry = ddlCountry.Items.FindByText(_address[4]);
                            if (_lstCountry != null)
                            {
                                ddlCountry.Text = _lstCountry.Value.ToString();
                            }

                            txtPostCode.Text = _address[5];
                        }
                        else
                        {
                            txtStreetName.Text = _enquiry.ContactAddress;
                        }
                    }
                }


                //GridViewRow _selectedRow = grvEnquiry.SelectedRow;
                //txtLocationCode.Text = _selectedRow.Cells[4].Text;
                //txtLocationName.Text = _selectedRow.Cells[5].Text;
                //txtDescription.Text = _selectedRow.Cells[6].Text;
                //lblMessage.Text = _selectedRow.Cells[4].Text;
                //CheckBox _chkStatus = (CheckBox)(_selectedRow.Cells[7].Controls[0]);
                //chkStatus.Checked = _chkStatus.Checked;
                //lblMessage.Text = _chkStatus.Checked.ToString();
            }
        }
    }

    private void ClearFields()
    {
        lblPageMessage.Text = "";
        lblSchoolKey.Text = ddlSchoolList.Text;
        lblLocationKey.Text = ddlLocation.Text;
        lblLocationKey.Text = "-1";
        lblEnquiryKey.Text = "-1";
        ddlGender.Text = "-1";

        txtFirstName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtOtherName.Text = string.Empty;
        txtContactEmail.Text = string.Empty;
        txtContactPhoneNo.Text = string.Empty;
        if (ddlEnquiryType.Items.Count > 0)
        {
            ddlEnquiryType.SelectedIndex = 0;
        }
        if (ddlEnquiryReasonType.Items.Count > 0)
        {
            ddlEnquiryReasonType.SelectedIndex = 0;
        }
        txtStreetName.Text = string.Empty;
        txtUnitNo.Text = string.Empty;
        if (ddlCountry.Items.Count > 0)
        {
            ddlCountry.SelectedIndex = 0;
        }
        if (ddlDistrict.Items.Count > 0)
        {
            ddlDistrict.SelectedIndex = 0;
        }
        txtPostCode.Text = string.Empty;

        txtMessage.Text = string.Empty;
        txtRemark.Text = string.Empty;
        btnCreateStudentProfile.Visible = false;
    }

    protected void ddlStandardLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnShowLocation_Click(object sender, EventArgs e)
    {

    }


    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        grvEnquiry.DataBind();
    }

    protected void btnCreateStudentProfile_Click(object sender, EventArgs e)
    {
        if (lblEnquiryKey.Text != "-1" && lblSchoolKey.Text != "-1" && lblLocationKey.Text != "-1")
        {
            Response.Redirect("~/student/?enqid=" + lblEnquiryKey.Text + "&locid=" + lblLocationKey.Text + "&schid=" + lblSchoolKey.Text);
        }
    }
}
